<%
   # Retrieve widget settings
   metric_data_map = {}
   metric_map = {}
   metrics_specified = false
   (1..3).each do |index|
     metric=widget_properties["metric#{index}"]
     # we check that the current resource has the selected metric on the last snapshot
     # => if not, we do not display this metric at all
     if metric
       metrics_specified = true
       if measure(metric)
         metric_data_map[metric.id] = []
         metric_map[metric.id] = metric
       end
     end
   end
   unless metrics_specified
     # No metric has been selected, it's the first time the widget is displayed: 'ncloc' is the default metric
     ncloc = Metric.by_name('ncloc')
     metric_data_map[ncloc.id] = []
     metric_map[ncloc.id] = ncloc
   end
   chartHeight = widget_properties["chartHeight"]

  unless metric_data_map.values.empty?
     # Retrieve metric trend information
     options = {}
     from_date = dashboard_configuration.from_datetime
     if from_date
       options[:from] = from_date
     end
     metric_count_per_snapshot_id = {}
     TrendsChart.time_machine_measures(@resource, metric_data_map.keys, options).each() do |trend_item|
       sid = trend_item["sid"]
       if metric_count_per_snapshot_id[sid]
         metric_count_per_snapshot_id[sid] += 1
       else
         metric_count_per_snapshot_id[sid] = 1
       end
       metric_data_map[trend_item["metric_id"].to_i] << {:date => trend_item["created_at"], :value => trend_item["value"], :sid => trend_item["sid"]}
     end

     # Create JS structures to print out in the HTML page
     js_data = "["
     js_snapshots = "["
     js_metrics = "["
     total_number_of_metrics = metric_map.keys.size()
     metric_data_map.keys.each_with_index() do |metric_id, index|
       unless metric_data_map[metric_id].empty?
         js_metrics += "\"" + metric_map[metric_id].short_name + "\","
         js_data += "["
         metric_data_map[metric_id].each() do |metric_data|
           # for every metric value, we need to check that the corresponding snapshot has values for each metric
           if metric_count_per_snapshot_id[metric_data[:sid]]==total_number_of_metrics
             m_date = metric_data[:date]
             # Only Oracle returns a Time object, so let's parse this string if it's not a Time instance
             m_date = Time.parse(metric_data[:date]) unless m_date.is_a? Time
             m_value = sprintf("%0.02f", metric_data[:value])
             m_value_localized = ProjectMeasure.new(:metric => metric_map[metric_id]).format_numeric_value(metric_data[:value], {})
             js_data += "{x:d("
             js_data += m_date.year.to_s
             js_data += ","
             # Need to decrease by 1 the month as the JS Date object start months at 0 (= January)
             js_data += (m_date.month - 1).to_s
             js_data += ","
             js_data += m_date.day.to_s
             js_data += ","
             js_data += m_date.hour.to_s
             js_data += ","
             js_data += m_date.min.to_s
             js_data += ","
             js_data += m_date.sec.to_s
             js_data += "),y:"
             js_data += m_value
             js_data += ",yl:\""
             js_data += m_value_localized
             js_data += "\"},"
             if index == 0
               # we fill the js_snapshots array (no need to do this more than once)
               js_snapshots += "{sid:"
               js_snapshots += metric_data[:sid].to_s
               js_snapshots += ",d:\""
               js_snapshots += human_short_date m_date
               js_snapshots += "\"},"
             end
           end
         end
         js_data = js_data.chomp(',') + "],"
       end
     end
     js_data = js_data.chomp(',') + "]"
     js_snapshots = js_snapshots.chomp(',') + "]"
     js_metrics = js_metrics.chomp(',') + "]"

     # Prepare also event structure if required
     unless widget_properties["hideEvents"]
       events = {}
       unless from_date
         # find the oldest date
         metric_data_map.values.each() do |metric_data_array|
           first_date = metric_data_array[0][:date]
           # Only Oracle returns a Time object, so let's parse this string if it's not a Time instance
           first_date = Time.parse(metric_data_array[0][:date]) unless first_date.is_a? Time
           from_date = first_date if !from_date || from_date > first_date
         end
       end
       Event.find(:all, :conditions => ["resource_id=? AND event_date>=?", @resource.id, from_date], :order => 'event_date').each() do |event|
         if events[event.event_date]
           events[event.event_date] << event
         else
           date_entry = [event]
           events[event.event_date] = date_entry
         end
       end
       js_events = "["
       events.keys().sort.each() do |e_date|
         e_details = events[e_date]
         js_events += "{sid:"
         js_events += e_details[0].snapshot_id.to_s
         js_events += ",d:d("
         js_events += e_date.year.to_s
         js_events += ","
         # Need to decrease by 1 the month as the JS Date object start months at 0 (= January)
         js_events += (e_date.month - 1).to_s
         js_events += ","
         js_events += e_date.day.to_s
         js_events += ","
         js_events += e_date.hour.to_s
         js_events += ","
         js_events += e_date.min.to_s
         js_events += ","
         js_events += e_date.sec.to_s
         js_events += "),l:["
         e_details.each() do |e|
           js_events += "{n:\""
           js_events += json_escape(e.name)
           js_events += "\"},"
         end
         js_events = js_events.chomp(',') + "]},"
       end
       js_events = js_events.chomp(',') + "]"
     end
  end


%>

<!--[if lte IE 8 ]> <h3><%= message('widget.unsupported_browser_warning') -%></h3> <![endif]-->

<!--[if (gte IE 9)|!(IE)]><!-->
<% if widget_properties["chartTitle"] %>
  <h3 style="margin-bottom: 10px"><%= h(widget_properties["chartTitle"]) -%></h3>
<% end %>
<!--<![endif]-->


<!--[if (gte IE 9)|!(IE)]><!-->
<%
   unless metric_data_map.values.empty?

     if metric_data_map.values[0].size == 1
%>

  <span class="empty_widget"><%= message('widget.timeline.timeline_not_displayed') -%></span>

<%   else %>
  <% timeline = 'timeline' + widget.id.to_s %>

  <div id="timeline-chart-<%= widget.id -%>" style="height: <%= chartHeight -%>px;"></div>
  <script>
    function d(y, m, d, h, min, s) {
      return new Date(y, m, d, h, min, s);
    }
    var data = <%= js_data -%>;
    var snapshots = <%= js_snapshots -%>;
    var metrics = <%= js_metrics -%>;
    var events = <%= js_events ? js_events : "[]" -%>;
    var <%= timeline -%> = new SonarWidgets.Timeline('timeline-chart-<%= widget.id -%>')
      .height(<%= chartHeight -%>)
      .data(data)
      .snapshots(snapshots)
      .metrics(metrics)
      .events(events);
    <%= timeline -%>.render();

    autoResize(200, function() {
      <%= timeline -%>.update();
    });
  </script>

<%
     end
   end
%>
<!--<![endif]-->
